require(['grid', 'topBar', 'layout','select2'], function () {
	var $top = $('#top'), $grid = $('#grid'),selLine,corpIds=[];
	
    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                   {
                       text: '车牌号',
                       name: 'licPltNo',
                       width: 80,
                       type: 'text'
                   },
                   {
                       text: '生产厂家',
                       name: 'produceFacty',
                       width: 80,
                       type: 'text'
                   },
                   {
                    text: '查询',
                    type: 'btn',
                    click: function () {
        				var nodes = commonFun.getLeftTreeCheckedNodes();
        				var includeLine = false,isSoloLine=true;
        				//判断左侧树是否选择相应节点 判断是否打开弹出框进行操作
        				for(var key in nodes){
        					if(key=='LINE'){
        						includeLine = true;
        						selLine = nodes[key];
        						if(selLine.length>1){
        							isSoloLine = false;
        						}
        					}
        					if(key=='CAR'){
        						selCar = nodes[key];
        					}
        				}
        				if(includeLine){//是否包含线路
        					if(!isSoloLine){
        						$.messager.alert("警告","请选择单条线路!","info");
        					}else{
        						reloadCarList(selLine.toString());
        					}
        				}else{
        					$.messager.alert("警告","请选择线路!","info");
        				}
                    	
                    }
                }
            ]
        });
    }
    //grid初始化
    function gridInit() {
	    //未绑定线路车辆列表
	    $grid.grid({
	        url: "/lineCarRel/getUnboundCar.htm",
	        colModel: [
	            {name: "carId",label: 'id', key: true, hidden: true},
	            {name: "corpId",label: 'corpId', hidden: true},
	            {name: "licPltNo",label: '车牌号', width: 80},
	            {name: "eqmtCd",label: '设备号', width: 80},
	            {name: "organName",label: '所属部门', width: 80},
	            {name: "produceFacty",label: '生产厂家', width: 80},
	            {name: "prchDate",label: '购置日期', width: 100,align:'right'},
	            {name: "frameNo",label: '车架号(VIN码)', width: 80},
	            {name: "carType",label: '车辆类型', width: 80,dict: "CAT00000"},
	            {name: "flOilType",label: '燃油种类', width: 80, dict: "FUT00000"},
	            {name: "emissiGrade",label: '排放等级', width: 80, dict: "EML00000"},
	            {name: "tankCapty",label: '油箱容量', width: 80,align:'right'},
	            {name: "refrEnerCons",label: '标准能耗', width: 80,align:'right'}
	        ],
	        multiselect: true
	    });
    }

   add = function(fn) {
	   var carIds = $grid.grid('getGridParam','selarrrow');
	   if(carIds.length<=0){//判断如果没选车辆提示
	   		$.messager.alert("警告","请勾选添加的车辆!","error");
	   }else{
		   	$.each(carIds,function(i){
		   		corpIds.push($grid.grid('getRowData',carIds[i]).corpId);
		   	});
		   	var lineId = commonFun.getLeftTreeCheckedNodes("LINE").toString();
		   	//保存线路车辆关联动作
		   	$.ajaxExt({
		   		url:"/lineCarRel/saveLineCarRel.htm",
		   		data:{'carIds' :carIds,'lineId' :lineId,'corpIds' :corpIds}
		   	}).done(function(){
		   		if(fn){
					fn();
				}
		   	});
	   }
	};
    
	//重新载入线路车辆绑定动作
	reloadCarList = function(selLine){
		$grid.grid('setGridParam', {page:1,datatype:"json",postData: {"lineId":selLine,"licPltNo":$top.getValues().licPltNo,
			"produceFacty":$top.getValues().produceFacty}}).trigger("reloadGrid");
	};
	
    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});